﻿using TestWebApplication.CommerceAccount.APIs;
using Microsoft.Commerce.Proxy.AccountService;
using System.ServiceModel;
using System;
using System.Security;
using System.Security.Cryptography.X509Certificates;

namespace TestWebApplication.CommerceAccount
{

    public class CommerceAccountServiceAdapter : ServiceAdapter
    {
        private const string CommerceContractName = "Microsoft.Commerce.Proxy.AccountService";

        public CommerceAccountServiceAdapter(string serviceUrl, X509Certificate2 cert)
            : base(serviceUrl, cert)
        {

        }

        public string TestConnection(string input)
        {
            ChannelFactory<AccountServiceChannel> clientFactory = new ChannelFactory<AccountServiceChannel>(this.binding, this.address);

            clientFactory.Endpoint.Contract.Name = CommerceContractName;
            clientFactory.Credentials.ClientCertificate.Certificate = this.clientCert;

            AccountServiceChannel channel = clientFactory.CreateChannel();

            string output = channel.TestConnection(input);

            return output;
        }

        public CreateAccountOutput CreateAccount(CallerInfo callerInfo, AccountInfo accountInfo)
        {
            CreateAccountInput input = PrepareCreateAccountInput(callerInfo, accountInfo);

            ChannelFactory<AccountServiceChannel> clientFactory = new ChannelFactory<AccountServiceChannel>(this.binding, this.address);

            clientFactory.Endpoint.Contract.Name = CommerceContractName;
            clientFactory.Credentials.ClientCertificate.Certificate = this.clientCert;
            AccountServiceChannel channel = clientFactory.CreateChannel();

            CreateAccountOutput output = channel.CreateAccount(input);


            return output;
        }

        public GetAccountOutput GetAccount(string accountId, CallerInfo callerInfo)
        {
            GetAccountInput input = PrepareGetAccountInput(callerInfo, accountId);

            ChannelFactory<AccountServiceChannel> clientFactory = new ChannelFactory<AccountServiceChannel>(this.binding, this.address);

            clientFactory.Endpoint.Contract.Name = CommerceContractName;
            clientFactory.Credentials.ClientCertificate.Certificate = this.clientCert;
            AccountServiceChannel channel = clientFactory.CreateChannel();

            GetAccountOutput output = channel.GetAccount(input);


            return output;

            
            
        }

        public GetAccountInput PrepareGetAccountInput(CallerInfo callerInfo, string accountId)
        {
            GetAccountInput input = new GetAccountInput()
            {
                APIContext = new APIContext()
                {
                    TrackingGuid = Guid.NewGuid(),
                },
                CallerInfo = callerInfo,
                SearchCriteria = new AccountSearchCriteria()
                {
                    AccountId = accountId,
                    Identity = callerInfo.Requester
                }
            };

            return input;
        }

        public CreateAccountInput PrepareCreateAccountInput(CallerInfo callerInfo, AccountInfo accountInfo)
        {
            // prepare the create account input;
            CreateAccountInput input = new CreateAccountInput()
            {
                APIContext = new APIContext()
                {
                    TrackingGuid = Guid.NewGuid()
                },
                CallerInfo = callerInfo,
                AccountInputInfo = accountInfo,
            };

            return input;
        }
    }
}
